package com.zhou.wx;

import com.alibaba.fastjson.JSON;
import com.zhou.bean.Express;
import com.zhou.bean.Message;
import com.zhou.bean.nExpress;
import com.zhou.mvc.ResponseBody;
import com.zhou.mvc.ResponseView;
import com.zhou.service.ExpressService;
import com.zhou.utils.DateFormatUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QRcodeController {

    @ResponseView("/wx/qrcode.do")
    public String qrcode(HttpServletRequest req, HttpServletResponse resp) {
        String code = req.getParameter("code");
        //express
        String type = req.getParameter("type");
        String phone = null;
        String qrcodeContent = null;
        if (type.equals("user")) {
            //用户二维码：被扫后在展示用户所有快递
            //phone
            phone = (String) req.getSession().getAttribute("phone");
            qrcodeContent = "userPhone_" + phone;
        } else {
            //快递二维码：展示单个快递信息
            //code
            qrcodeContent = "express_" + code;
        }
        req.getSession().setAttribute("qrcode", qrcodeContent);
        return "/personQRcode.html";
    }

    @ResponseBody("/wx/getqrcode.do")
    public String getQrCode(HttpServletRequest req, HttpServletResponse resp) {
        String qrcode = (String) req.getSession().getAttribute("qrcode");
        Message message = new Message();
        if (qrcode == null) {
            message.setStatus(-1);
            message.setResult("取件码获取出错，请重新操作");
        } else {
            message.setStatus(0);
            message.setResult(qrcode);
        }

        return JSON.toJSONString(message);
    }

    @ResponseBody("/wx/updateStatus.do")
    public String updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        String code = req.getParameter("code");
        boolean b = ExpressService.updateStatus(code);
        Message message = new Message();
        if (b) {
            message.setStatus(0);
            message.setResult("取件成功");
        } else {
            message.setStatus(-1);
            message.setResult("取件失败，请用户更新二维码");
        }
        return JSON.toJSONString(message);
    }
    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest req, HttpServletResponse resp) {
        String code = req.getParameter("code");
        Express express = ExpressService.findByCode(code);
        Message message = new Message();
        if (express.getCode() == null){
            message.setStatus(-1);
            message.setResult("取件码不存在");
        }else {
            message.setStatus(0);
            message.setResult("查询成功");
            String inTime = DateFormatUtil.format(express.getInTime());
            String outTime = express.getOutTime()==null?"未出库":DateFormatUtil.format(express.getOutTime());
            String status = express.getStatus()==0?"未取件":"已取件";
            String code1 = express.getCode()==null?"已取件":express.getCode();
            nExpress nExpress = new nExpress(express.getId(), express.getNumber(), express.getUsername(), express.getUserPhone(), express.getCompany(), code1,inTime,outTime,status,express.getSysPhone());
            message.setData(nExpress);
        }
        return JSON.toJSONString(message);
    }
}
